package cn.tedu.hospitalgroupproject.server.content.service.impl;

import cn.tedu.hospitalgroupproject.server.common.ex.ServiceException;
import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.ServiceCode;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IClinicRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDepartmentRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Department;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DepartmentAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DepartmentUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;

@Slf4j
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Value("${hospital-appointment.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IDepartmentRepository departmentRepository;
    @Autowired
    private IClinicRepository clinicRepository;

    public DepartmentServiceImpl() {
        log.info("创建业务对象:DepartmentServiceImpl");
    }


    @Override
    public void addNew(DepartmentAddNewParam departmentAddNewParam) {
        log.debug("开始处理[新增科室]的业务,参数:{}",departmentAddNewParam);
        Department department=new Department();
        BeanUtils.copyProperties(departmentAddNewParam,department);
        departmentRepository.insert(department);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除科室]的业务,诊所id:{}",id);
        int count=clinicRepository.countByDeptId(id);
        if(count>0){//当前要删除的科室中还有存在诊所
            String message = "删除科室失败，尝试删除的科室下还存在诊所！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //当没有医生时执行删除
        departmentRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(DepartmentUpdateInfoParam departmentUpdateInfoParam) {
        log.debug("开始处理[根据指定ID更新对应科室]的业务");
        DepartmentStandardVO currentVO=departmentRepository.getStandardById(departmentUpdateInfoParam.getId());
        if(currentVO==null){
            String message="更新失败!想要更新的指定ID的对应数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        int count=departmentRepository.countByNameAndNotId(departmentUpdateInfoParam.getId(),departmentUpdateInfoParam.getName());
        if(count>0){
            String message="更新失败!想要更新的新科室名已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        Department department=new Department();
        BeanUtils.copyProperties(departmentUpdateInfoParam,department);
        int rows=departmentRepository.updateById(department);
        if(rows!=1){
            String message="更新失败!服务器忙请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum) {
        log.debug("开始处理【查询科室列表】业务，页码：{}", pageNum);
        PageData<DepartmentListVO> pageData = departmentRepository.list(pageNum, defaultQueryPageSize);
        return pageData;

    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询科室列表】业务，页码：{}", pageNum);
        PageData<DepartmentListVO> pageData = departmentRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
       log.debug("开始处理[根据指定ID查询指定科室数据]业务,id:{}",id);
       DepartmentStandardVO currentVO=departmentRepository.getStandardById(id);
       if(currentVO==null){
           String message="指定ID下的科室内容不存在!";
           log.debug(message);
           throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
       }
        return currentVO;
    }
}
